package com.fhzn.auth.service.impl;

import com.alibaba.fastjson.JSON;
import com.fhzn.auth.entity.OperateLog;
import com.fhzn.auth.mapper.OperateLogMapper;
import com.fhzn.auth.service.OperatorLogService;
import com.fhzn.commons.toolkit.exception.BuzException;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/25
 */
@Service
@AllArgsConstructor
public class OperatorLogServiceImpl implements OperatorLogService {

    private final OperateLogMapper operateLogMapper;

    public void operatorLog(String handler, String operateSection, String operateMethod,
                            Object operateTarget, Object objBefore, Object objAfter, String loginSystem) {
        try {
            OperateLog operateLog = new OperateLog();
            operateLog.setHandler(handler);
            operateLog.setOperateSection(operateSection);
            operateLog.setOperateMethod(operateMethod);
            operateLog.setOperateTarget(String.valueOf(operateTarget));
            operateLog.setModifyBeforeContent(StringUtils.substring(JSON.toJSONString(objBefore), 0, 1000));
            operateLog.setModifyAfterContent(StringUtils.substring(JSON.toJSONString(objAfter), 0, 1000));
            operateLog.setLoginSystem(loginSystem);
            operateLogMapper.insert(operateLog);
        } catch (Exception e) {
            throw new BuzException("记录操作日志出现错误", e);
        }
    }
}
